/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.kairos.motion.strategy
{ 
	/**
	 * Play an animation and loop at the end.
	 * 
	 * <p><code>LoopStrategy</code> extends <code>NoLoopStrategy</code> 
	 * and add the loop routine.</p>
	 * 
	 * <p>The strategy will skip the last frames when the distance
	 * between the current frame and the last frame is smaller than
	 * the current playhead's move step.</p>
	 * 
	 * @author 	Cédric Néhémie
	 * @see		PlayStrategy
	 */
	public class LoopStrategy extends NoLoopStrategy implements PlayStrategy 
	{
		/**
		 * The <code>LoopStrategy.isMotionFinished</code>method allways return false
		 * because looping animations never stop by theirself.
		 * 
		 * @param	frame		The current frame in the <code>MovieClip</code>
		 * @param	totalFrames	The number of frames in the <code>MovieClip</code>
		 * @param	step		How many frames the playhead have to move.
		 * @return	Allways <code>false</code>
		 */
		public override function isMotionFinished(frame : uint, totalFrames : uint, step : uint) : Boolean 
		{
			return false;
		}
		
		/**
		 * Returns the next frame wherein the playhead can move.
		 * 
		 * <p>When the playhead is going to go past the last frame
		 * the animation loop and the playhead follow its move from
		 * first frame.</p>
		 * 
		 * @param	frame		The current frame in the <code>MovieClip</code>
		 * @param	totalFrames	The number of frames in the <code>MovieClip</code>
		 * @param	step		How many frames the playhead have to move.
		 * @return	A valid frame index in the <code>MovieClip</code>
		 */
		public override function getNextFrame(frame : uint, totalFrames : uint, step : uint) : uint 
		{
			return _isMotionLoop ( frame, totalFrames, step ) ?
						(frame + step) % totalFrames :
						frame + step;
		}
		
		
		/**
		 * Used internally to call the <code>NoLoop.isMotionFinished</code> method
		 * in order to loop correctly.
		 * 
		 * @param	frame		The current frame in the <code>MovieClip</code>
		 * @param	totalFrames	The number of frames in the <code>MovieClip</code>
		 * @param	step		How many frames the playhead have to move.
		 * @return	<code>true</code> if the animation have to stop after the
		 * 			playhead moves, either <code>false</code>
		 */
		protected function _isMotionLoop ( frame : uint, totalFrames : uint, step : uint ) : Boolean
		{
			return super.isMotionFinished( frame, totalFrames, step );
		}
		
	}
}